using System.Collections.Generic;
using System.IO;
using ColliderSystem;
using NFixedPoint;
using UnityEditor;
using UnityEngine;

namespace Demo.Assets.Editor
{
    public class GenColliderEditor
    {
        static List<ShapeInfo> list = new List<ShapeInfo>();

        [MenuItem("NNTool/GenColliderInfo")]
        private static void SaveInfo()
        {
            //拿到场景中box
            var box = GameObject.Find("ColliderRoot").GetComponentsInChildren<BoxCollider>();
            foreach (var b in box)
            {
                var tran = b.transform;
                //转成shapeinfo
                if (b.gameObject.activeSelf == false)
                {
                    continue;
                }

                var info = new BoxInfo()
                {
                    position = tran.position.ToFixedPoint3(),
                    uid = tran.gameObject.GetInstanceID(),
                    size = FixedPoint3.Multiply(
                        (tran.localScale / 2).ToFixedPoint3(),
                        b.size.ToFixedPoint3()
                    ),
                    axis = new FixedPoint3[]
                    {
                        tran.right.ToFixedPoint3(),
                        tran.up.ToFixedPoint3(),
                        tran.forward.ToFixedPoint3()
                    }
                };
                list.Add(info);
            }
            //拿到场景中Sphere
            var sphere = GameObject.Find("ColliderRoot").GetComponentsInChildren<SphereCollider>();

            foreach (var s in sphere)
            {
                Transform tran = s.transform;
                if (tran.gameObject.activeSelf == false)
                {
                    continue;
                }

                var info = new SphereInfo()
                {
                    position = tran.position.ToFixedPoint3(),
                    uid = tran.gameObject.GetInstanceID(),
                    radius = (FixedPoint)(s.radius * tran.localScale.x),
                };
                list.Add(info);
            }

            Save();
        }

        static void Save()
        {
            //info 序列化保存二进制
            NNTools.SerializeObject<List<ShapeInfo>>(
                list,
                Application.persistentDataPath + CommonConst.ColliderPath
            );
            Debug.LogError("【保存场景碰撞信息成功】" + list);
        }
    }
}
